package com.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.DistributionMapper;
import com.pojo.*;
import com.vo.QueryVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DistributionService {
    
    @Resource
    private DistributionMapper distributionMapper;
    @Resource
    private ClientService clientService;
    
    @Transactional (propagation = Propagation.REQUIRES_NEW, readOnly = true)
    public Distribution queryByKey(int d_num){
        return distributionMapper.selectByPrimaryKey(d_num);
    }
    
    @Transactional (propagation = Propagation.REQUIRES_NEW, readOnly = true)
    public List<Distribution> queryByType(Distribution distribution){
        DistributionExample example = new DistributionExample();
        DistributionExample.Criteria criteria = example.createCriteria();
        if (distribution != null){
            if (distribution.getC_id()!= null) {
                criteria.andC_idEqualTo(distribution.getC_id());
            }
            if (distribution.getO_id() != null && !"".equals(distribution.getO_id().trim())) {
                criteria.andO_idEqualTo(distribution.getO_id().trim());
            }
        }
        return distributionMapper.selectByExample(example);
    }
    
    @Transactional (propagation = Propagation.REQUIRES_NEW, readOnly = true)
    public PageInfo<Distribution> queryByPage(Integer PageNum , Integer PageSize , QueryVo vo) {
        
        //约束查询
        DistributionExample example = new DistributionExample();
        DistributionExample.Criteria criteria = example.createCriteria();
        if (vo != null) {
            if (vo.getO_id() != null && !"".equals(vo.getO_id().trim())) {
                criteria.andO_idEqualTo(vo.getO_id().trim());
            }
            //c_name 需要查id
            if (vo.getC_name() != null && !"".equals(vo.getC_name().trim())) {
                Client client = null;
                Client tamp = new Client();
                tamp.setC_name(vo.getC_name());
                List<Client> clients = clientService.queryByType(tamp);
                if (!clients.isEmpty()){
                    client = clients.get(0);
                }else {
                    client = new Client();
                    client.setC_id(-1);
                }
                criteria.andC_idEqualTo(client.getC_id());
            }
            if (vo.getBeginDate()!=null){
                criteria.andD_timeGreaterThanOrEqualTo(vo.getBeginDate());
            }
            if (vo.getEndDate()!=null){
                criteria.andD_timeLessThan(vo.getEndDate());
            }
        }
        
        //填充数据 c_name
        PageHelper.startPage(PageNum,PageSize);
        List<Distribution> list = distributionMapper.selectByExample(example);
        for (Distribution d : list) {
            
            Client c = new Client();
            Client client = null;
            c.setC_id(d.getC_id());
            List<Client> clients = clientService.queryByType(c);
            if (!clients.isEmpty()) {
                client = clients.get(0);
            }
            d.setC_name(client.getC_name());
            
        }
        
        list.forEach(distribution -> System.out.println("\tdistribution : " + distribution));
        
        return new PageInfo<>(list);
    }
    
    @Transactional (propagation = Propagation.REQUIRES_NEW, readOnly = true)
    public List<Distribution> queryByDate(QueryVo vo){
        DistributionExample example = new DistributionExample();
        DistributionExample.Criteria criteria = example.createCriteria();
        if (vo != null) {
            if (vo.getBeginDate() != null) {
                criteria.andD_timeGreaterThanOrEqualTo(vo.getBeginDate());
            }
            if (vo.getEndDate() != null) {
                criteria.andD_timeLessThanOrEqualTo(vo.getEndDate());
            }
        }
        return distributionMapper.selectByExample(example);
    }
    
    @Transactional (propagation = Propagation.REQUIRED , rollbackFor = {Exception.class})
    public int addSelective(Distribution distribution){
        return distributionMapper.insertSelective(distribution);
    }
    
    @Transactional (propagation = Propagation.REQUIRED , rollbackFor = {Exception.class})
    public int delByKey(Integer d_num) {
        return distributionMapper.deleteByPrimaryKey(d_num);
    }
    
    @Transactional (propagation = Propagation.REQUIRED , rollbackFor = {Exception.class})
    public int update(Distribution distribution) {
        return distributionMapper.updateByPrimaryKeySelective(distribution);
    }
    
    
}
